package search;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/4/10
 * Time: 18:35
 */
public class InsertValueSearch {

    public static void main(String[] args) {
        int[] arr = {1,8,10,89,100,200};
        int index = insertValueSearch(arr,0,arr.length-1,8);
        System.out.println(index);
    }

    public static int insertValueSearch(int[] arr, int left, int right, int findVal) {
        if(left > right || findVal < arr[0] || findVal > arr[arr.length-1]) {
            return -1;
        }
        int mid = left + (right - left) * (findVal - arr[left]) / (arr[right] - arr[left]);
        if(findVal > arr[mid]) {
            return insertValueSearch(arr, mid+1, right, findVal);
        }else if(findVal < arr[mid]) {
            return insertValueSearch(arr, left, mid-1, findVal);
        }else {
            return mid;
        }
    }
}
